﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{
	#region 单例模型
	private static CameraController singleton;
	private CameraController() { }
	private void Awake() { singleton = this; }
	public static CameraController Singleton { get => singleton; }

	#endregion

	[Header("鼠标左键拖拽速度")]
	public float moveSpeed;

	[Header("鼠标中键缩放速度")]
	public float wheelSpeed;

	[Header("鼠标右键旋转速度")]
	public float totateSpeed;

	[Header("看向目标点的转向速度")]
	public float lookspeed;

	[Header("点击选择的目标")]
	public Transform target;
	private Transform defaultTarget;    // 当target为空时, 看向上一个默认target

	private bool isLooking;
	private bool allowMove; // 是否允许平移
	public bool isAutoRotate = true;   // 开始之前, 相机自动旋转预览

	// public bool IsAutoRotate { get; }

	private UIController uICon; // UI Controller的单例

	private void Start() {
		isLooking = false;
		uICon = UIController.Singleton;
	}

	// Update is called once per frame
	private void Update() {

		if (isAutoRotate) { // 如果还在开始前的自动旋转
			transform.LookAt(new Vector3(13, 0, 17));
			transform.Translate(Vector3.right * Time.deltaTime * 5);
			return;
		}

		// 滑动滑轮放大缩小视野
		float wheel = Input.GetAxis("Mouse ScrollWheel");
		transform.Translate(Vector3.forward * Time.deltaTime *
			wheelSpeed * wheel * 50, Space.Self);

		// 右键旋转视野
		if (Input.GetKey(KeyCode.Mouse1)) {
			float mouseX = Input.GetAxis("Mouse X");
			transform.Rotate(Vector3.up * Time.deltaTime *
				totateSpeed * mouseX * 30, Space.World);
			isLooking = false;
			uICon.HidePopPanel();
			uICon.HideInfo();
			if (target) target.GetComponent<TowerBase>().DeleteVFX();
		}

		// 鼠标左键平移视野, 进入平移 // 起始点击点不在UI上在
		if (Input.GetKeyDown(KeyCode.Mouse0) && !uICon.isUI) {
			allowMove = true;
		}


		// 鼠标左键平移视野, 平移中
		if (allowMove && Input.GetKey(KeyCode.Mouse0)) {
			float mouseX = Input.GetAxis("Mouse X");
			transform.Translate(Vector3.left * Time.deltaTime *
				moveSpeed * mouseX * 2, Space.Self);
		}

		// 选择塔台
		if (Input.GetKeyUp(KeyCode.Mouse0)) {
			allowMove = false;

			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			if (!uICon.isUI && Physics.Raycast(ray, out hit)) {
				uICon.HideInfo();
				if (target) target.GetComponent<TowerBase>().DeleteVFX();
				target = hit.transform;
				defaultTarget = target;
				isLooking = true;
				// ；判断当前基座上是否创建防御塔
				if (target.GetComponent<TowerBase>().tower) {
					uICon.ShowPopPanel(UIState.Update);
				} else
					uICon.ShowPopPanel(UIState.Select);


				target.GetComponent<TowerBase>().CreateVFX();
			}
			// 	Debug.Log("鼠标选中了" + hit.transform.name);
		}


		// 视野缓慢看向塔台
		if (isLooking && target)
			UpdataLookAtTarget(target);

		// 按下ESC键取消选择
		if (Input.GetKeyDown(KeyCode.Escape)) {
			target = null;
			uICon.HidePopPanel();
			uICon.HideInfo();
			target.GetComponent<TowerBase>().DeleteVFX();
		}

		// 按住空格键, 视野回到中央
		if (Input.GetKey(KeyCode.Space) && defaultTarget) {
			UpdataLookAtTarget(defaultTarget);
		}
	}

	/// <summary>
	/// 看向目标点, 方法放在Update函数中
	/// </summary>
	/// <param name="ta"></param>
	private void UpdataLookAtTarget(Transform ta) {

		float dis = Vector3.Distance(transform.position, target.position);
		Vector3 lookingPos = transform.position + transform.forward * dis;

		Vector3 lookedPos = Vector3.Lerp(lookingPos, target.position,
			Time.deltaTime * lookspeed);

		transform.LookAt(lookedPos);    // 看向差值的点
		if (Vector3.Distance(lookedPos, target.position) < 0.01f) {
			isLooking = false;
			transform.LookAt(target);
		}
	}
}
